import {
  getDjCatelist,
  getUserAudio,
  getProgramRecommend,
  getDjProgramToplist,
  getDjRecommendType,
  getProgramDetail,
  getDjDetail,
  getDjSub
} from '@/api/radio/index'
import {getDjPersonalizeRecommend, getDjProgram, getDjRadioHot, getResourceLike} from "../../api/radio/index.js";




const radioState = {}
const radioActions = {
  // 获取用户创建的电台
  getUserAudio(uid) {
    return  getUserAudio(uid).then(res => {
      if (Number(res.code) === 200) {
        return res
      }
    })
  },
  // 主播电台分类
  getDjCatelist() {
   return getDjCatelist().then(res => {
      const { categories, code } = res
      if (Number(code) === 200) {
        return categories
      }
    })
  },
  // 推荐节目
  getProgramRecommend() {
    return getProgramRecommend().then(res => {
      if(Number(res.code) === 200) {
        return res
      }
    })
  },
  // 电台详情
  getDjDetail(id) {
    return getDjDetail(id).then(res => {
      const { code, data } = res
      if(Number(code) === 200) return data
    })
  },
  // 获取电台节目详情用于展示页面信息
  getProgramDetail(id) {
    return getProgramDetail(id).then(res => {
      const { code, program } = res
      if (Number(code) === 200) {
        return program
      }
      // console.log(res)
    })
  },
  getProgramDetailPlay(id) {
    return getProgramDetail(id).then(async res => {
      console.log(res)
      if (Number(res.code) === 200)
      await this.setDetachment([res.program])
    })
  },
  // 电台节目榜
  getDjProgramToplist(params = { limit: 10, offset: 0 }) {
    return getDjProgramToplist(params).then(res => {
      const { code, toplist } = res
      if (Number(code) === 200) {
        return toplist
      }
    })
  },
  // 电台分类推荐
  getDjRecommendType(type) {
    return getDjRecommendType(type).then(res => {
      const { code, djRadios } = res
      if (Number(code) === 200) {
        return djRadios
      }
    })
  },
  // 订阅电台或者取消订阅电台 rid电台id t 1：订阅 0：取消
  getDjSub(rid, t) {
    return getDjSub({ rid: rid, t: t }).then(res => {
      if (Number(res.code) === 200) return res.code
    })
  },
  // 电台详情数据用来展示数据
  getDjProgram(params = { rid: null, asc: false }, num = 40) {
    return getDjProgram({ limit: num, offset: 0, ...params }).then(res => {
      if (Number(res.code) === 200) {
        return res
      }
    })
  },
  // 电台节目用于播放
  getDjProgramPlayer(params = { rid: null, asc: false }, limit = 40) {
    getDjProgram({ limit, ...params }).then(async res => {
      console.log(res.programs)
      if (Number(res.code) === 200) {
        await this.setDetachment(res.programs)
      }
    })
  },
  // 可能喜欢的电台
  getDjPersonalizeRecommend() {
    return getDjPersonalizeRecommend().then(res => {
      const { code, data } = res
      if (Number(code) === 200) {
        return data
      }
    })
  },
  // 电台热门类别
  getDjRadioHot(params = { cateId: null }, num = 0) {
    return getDjRadioHot({...params, limit: 30, offset: 40 * num }).then(res => {
      if(Number(res.code) === 200) {
        return res
      }
    })
  },
  // 1: mv 4: 电台 5: 视频 6: 动态
  // t: 操作,1 为点赞,其他为取消点赞
  // id: 资源 id
  // 如给动态点赞，不需要传入 id，需要传入 threadId
  getResourceLike(params) {
   return getResourceLike(params).then(res => {
     if (+res.code === 200) {
       return res.code
     }
    })
  }

}
const radioGetters = {}

export {
  radioActions,
  radioState,
  radioGetters
}
